package com.auth.web;

import com.alibaba.fastjson.JSON;
import com.auth.token.app.AppTokenClient;
import com.auth.token.app.UserInfo;
import com.auth.token.base.TokenGroup;
import com.auth.web.auth.MustLogin;
import com.auth.web.auth.UserContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author: 王霄
 * @date: 2022/1/20 21:32
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    private AppTokenClient appTokenClient;

    @GetMapping("/login")
    public TokenGroup login(String name, Integer age) {
        UserInfo userInfo = UserInfo.builder()
                .uId(1L)
                .name(name)
                .age(age)
                .build();
        TokenGroup tokenGroup = appTokenClient.askToken(userInfo);
        log.info(JSON.toJSONString(tokenGroup));
        return tokenGroup;
    }

    @GetMapping("/getData")
    @MustLogin
    public UserInfo getData() {
        return UserContext.getUserInfo();
    }

    @GetMapping("/refreshToken")
    public TokenGroup refreshToken(String refreshToken) {
        TokenGroup tokenGroup = appTokenClient.refreshToken(refreshToken);
        log.info(JSON.toJSONString(tokenGroup));
        return tokenGroup;
    }
}